package com.hangzhou.welbeing.welbeinginstrument.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hangzhou.welbeing.welbeinginstrument.R;
import com.hangzhou.welbeing.welbeinginstrument.adapter.PayAttentionAdapte;
import com.hangzhou.welbeing.welbeinginstrument.base.MyApplication;
import com.hangzhou.welbeing.welbeinginstrument.bean.PayAttentionBean;
import com.hangzhou.welbeing.welbeinginstrument.config.RBConstants;
import com.hangzhou.welbeing.welbeinginstrument.inteface.OnRecyclerViewItemClickListener;
import com.hangzhou.welbeing.welbeinginstrument.utils.Utils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 时间 2017 年 09 月 11.
 * 作者 Administrator  刘传华
 * 公司 杭州惟君医疗科技有限公司
 * 包名 com.hangzhou.welbeing.pregnant.welbeing_pregnant.activity
 * 工程名 welbeing_pregnant
 *
 * @desc 关注
 */
public class PayAttentionActivity extends Activity {
    private static final String TAG = "PayAttentionActivity" ;
    @Bind(R.id.activity_pay_attention_iv_return)
    ImageView activityPayAttentionIvReturn;
    @Bind(R.id.activity_pay_attention_RecyclerView)
    RecyclerView activityPayAttentionRecyclerView;
    private PayAttentionAdapte payAttentionAdapte;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay_attention);
        ButterKnife.bind(this);

        Utils.setStatusBar(this,false,false);
        Utils.setStatusTextColor(true,this);


        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        activityPayAttentionRecyclerView.setLayoutManager(layoutManager);



        loadPayAttentionDate(1);

    }
    /**
     * @desc 加载关注数据列表
     * */
    private void loadPayAttentionDate(int p) {
        SharedPreferences sp = getSharedPreferences("second_app_internal_data", 0);
        String user_id = sp.getString("user_id","");
        OkHttpUtils
                .post()
                .url(RBConstants.SERVER_URL+RBConstants.PAY_ATTENTION)
                .addParams("cus_id", user_id)
                .addParams("p",1+"")
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String praiseParseNetworkResponse =  response.body().string();
                        Gson gson = new Gson();
                       // Log.e(TAG,"praiseParseNetworkResponse=====23432=======>"+praiseParseNetworkResponse);
                        final PayAttentionBean payAttentionBean = gson.fromJson(praiseParseNetworkResponse,PayAttentionBean.class);
                        if(payAttentionBean.errorCode == 200 ){
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    payAttentionAdapte = new PayAttentionAdapte(payAttentionBean.data,PayAttentionActivity.this);
                                    activityPayAttentionRecyclerView.setAdapter(payAttentionAdapte);
                                    payAttentionAdapte.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
                                        @Override
                                        public void onItemClick(View view) {
                                            int position = activityPayAttentionRecyclerView.getChildAdapterPosition(view);
                                            Intent intent  =  new Intent(PayAttentionActivity.this,MyPostingOnCircleActivity.class);
                                            intent.putExtra("id",payAttentionBean.data.get(position).id);
                                            intent.putExtra("num","");
                                            intent.putExtra("att",payAttentionBean.data.get(position).att+"");
                                            intent.putExtra("cus_logo",payAttentionBean.data.get(position).cus_logo+"");
                                            intent.putExtra("cus_name",payAttentionBean.data.get(position).cus_name+"");
                                            startActivity(intent);
                                        }

                                        @Override
                                        public void onItemFirstButtonClick(View v, int position) {

                                        }
                                    });
                                }
                            });
                        }else{
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(MyApplication.getMcontext(),payAttentionBean.errorMessage,Toast.LENGTH_SHORT).show();
                                }
                            });
                        }

                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });

    }

    @OnClick(R.id.activity_pay_attention_iv_return)
    public void onViewClicked() {
        finish();
    }
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    }
}
